package com.mall.b2c.service.impl;

import com.mall.b2c.dao.CardDao;
import com.mall.b2c.dao.CommentDao;
import com.mall.b2c.dao.IntergralDao;
import com.mall.b2c.dao.MemberDao;
import com.mall.b2c.domain.card.CardDomain;
import com.mall.b2c.domain.intergral.IntergralDomain;
import com.mall.b2c.domain.intergral.IntergralLogDomain;
import com.mall.b2c.domain.member.MemberDomain;
import com.mall.b2c.enums.IntergralLogType;
import com.mall.b2c.service.*;
import com.mall.b2c.util.RandomPasswordUtil;
import com.mall.common.bean.Message;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.impl.BaseServiceImpl;
import com.mall.common.util.PasswordUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.LockModeType;
import java.util.Date;

/**
 * Created by chen on 16/12/15.
 */
@Service
@Transactional
public class CardServiceImpl extends BaseServiceImpl<CardDomain, Long> implements CardService {
    @Resource
    IntergralService intergralService;

    @Resource
    MemberService memberService;
    @Resource
    private CardDao cardDao;

    @Resource
    public void setBaseDao(CardDao cardDao) {
        super.setBaseDao(cardDao);
    }

    @Override
    public void jh(Long[] ids) {
        StringBuilder hql = new StringBuilder();
        hql.append("update CardDomain set isActivate =:isActivate where id in (");
        hql.append(StringUtils.join(ids, ","));
        hql.append(")");
        getEntityManager().createQuery(hql.toString()).setParameter("isActivate", true).executeUpdate();
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Message docardsubmit(CardDomain cardDomain, String password, Long memberId) {
        cardDao.lock(cardDomain, LockModeType.PESSIMISTIC_WRITE);
        if (cardDomain.getIsUsed()) {
            throw new NoticeException("该卡已经使用");

        }
        if (!cardDomain.getIsActivate()) {
            throw new NoticeException("卡尚未激活");
        }
        if (!cardDomain.getPassword().equals(password)) {
            throw new NoticeException("卡不存在或密码错误");
        } else {
            MemberDomain memberDomain = memberService.find(memberId);

            intergralService.alterations(memberId, cardDomain.getIntergral(), IntergralLogType.addnum, "使用充值卡充值 充值卡单号:" + cardDomain.getAccount());
            memberService.rechargeWithCard(memberId, cardDomain.getMoney(), cardDomain.getId());
            cardDomain.setIsUsed(true);
            cardDomain.setMemberDomain(memberDomain);
            cardDomain.setOvertime(new Date());
            cardDomain.setIsActivate(true);
            update(cardDomain);
            return Message.success("充值成功", null);


        }


    }


    public void produceCard(Long number, CardDomain cardDomain) {
        //这里可以使用VALUES(),(),() 不熟悉 来不及先做了
        for (int i = 0; i < number; i++) {
            CardDomain temp = new CardDomain();
            temp.setIsUsed(false);
            temp.setPassword(RandomPasswordUtil.getPassword().toUpperCase());
            temp.setIntergral(cardDomain.getIntergral());
            temp.setMoney(cardDomain.getMoney());
            temp.setIsActivate(false);
            persist(temp);
            if (i % 20 == 0) {
                getBaseDao().flush();
                getBaseDao().clear();
            }

        }


    }
}





